package com.aistar.service.impl;

import com.aistar.mapper.CustomerMapper;
import com.aistar.pojo.Customer;
import com.aistar.pojo.CustomerExample;
import com.aistar.service.CustomerService;
import com.aistar.util.MessageUtil;
import com.aistar.util.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CustomerServiceImpl implements CustomerService {
    private CustomerMapper customerMapper;

    @Autowired
    public CustomerServiceImpl(CustomerMapper customerMapper) {
        this.customerMapper = customerMapper;
    }

    @Override
    public ServerResponse getById(Integer customerId) {
        Customer customer = customerMapper.selectByPrimaryKey(customerId);
        if (customer != null && !MessageUtil.STATUS_FAIL.equals(customer.getStatus()))
            return ServerResponse.getSuccess(customer);
        return ServerResponse.getFailed();
    }

    @Override
    public ServerResponse getByEmail(String email) {
        CustomerExample example = new CustomerExample();
        example.createCriteria()
                .andStatusEqualTo(MessageUtil.STATUS_SUCCESS)
                .andCustomerEmailEqualTo(email);
        List<Customer> customerList = customerMapper.selectByExample(example);
        if (customerList != null)
            if (customerList.size() == 1)
                return ServerResponse.getSuccess(customerList.get(0));
            else if (customerList.size() > 1)
                return ServerResponse.getFailed(MessageUtil.USER_ERROR);
        return ServerResponse.getFailed(MessageUtil.USER_NONE);
    }

    @Override
    public ServerResponse getByTel(Long telno) {
        CustomerExample example = new CustomerExample();
        example.createCriteria()
                .andStatusEqualTo(MessageUtil.STATUS_SUCCESS)
                .andCustomerTelEqualTo(telno);
        List<Customer> customerList = customerMapper.selectByExample(example);
        if (customerList != null)
            if (customerList.size() == 1)
                return ServerResponse.getSuccess(customerList.get(0));
            else if (customerList.size() > 1)
                return ServerResponse.getFailed(MessageUtil.USER_ERROR);
        return ServerResponse.getFailed(MessageUtil.USER_NONE);
    }

    @Override
    public ServerResponse add(Customer customer) {
        customer.setCustomerRegtime(new Date());
        int rows = customerMapper.insertSelective(customer);
        if (rows > 0)
            return ServerResponse.addSuccess();
        return ServerResponse.addFailed();
    }

    @Override
    public ServerResponse modify(Customer customer) {
        CustomerExample example = new CustomerExample();
        example.createCriteria()
                .andCustomerIdEqualTo(customer.getCustomerId());
        int rows = customerMapper.updateByExampleSelective(customer, example);
        if (rows > 0)
            return ServerResponse.modifySuccess();
        return ServerResponse.modifyFailed();
    }
}
